﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HZ.MBSM.BLL;
using HZ.MBSM.DBUtility;
public partial class Manage_LinkAdd : System.Web.UI.Page,IPageDescribe
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (HZ.MBSM.BLL.Login.CheckAssert(this.UseAccountType))
        {
            Show();

            if (Request.Form["btnSubmit"] == "删除" && Request.Form["chk"] != "" && Request.Form["chk"]!=null)
            {
                Db db = new Db();
                db.Command("delete from link where LinkId in (" + Request.Form["chk"] + ")");
                db.Close();
                Response.Write("<script>alert('删除成功！');</script>");
                Response.Write("<script>location.href='LinkAdd.aspx';</script>");
            }
        }
    }
    private void Show()
    {
        Db db = new Db();
        DbPage dbPage = new DbPage(db);
        dbPage.Table = "link";                                               //要查询的表明
        dbPage.Key = "LinkId";                                               //表的主键    
        dbPage.Where = "";                                           //查询条件    
        dbPage.SelectField = "LinkId,Title,LinkWWW,AddDate,case  when IsShow =0 then '显示' when IsShow=1 then '不显示' end  as IsShow";//要显示的字段
        dbPage.PageIndex = SysFun.ToInt(Request["CurrentlyPageIndex"]);          //要显示第几页   
        dbPage.PageSize = 5;                                                    //没有显示记录条数   
        dbPage.OrderBy = "AddDate";//排序字段


        System.Text.StringBuilder strHtml = new System.Text.StringBuilder();
        strHtml.Append("<table width='98%' border='0' align='center' cellpadding='2' cellspacing='1' class='table_table'>");
        strHtml.Append("<tr>");
        strHtml.Append("<td class='table_title' nowrap>链接编号</td>");
        strHtml.Append("<td class='table_title' nowrap>链接标题</td>");
        strHtml.Append("<td class='table_title' nowrap>链接地址</td>");
        strHtml.Append("<td class='table_title' nowrap>链接日期</td>");
        strHtml.Append("<td class='table_title' nowrap>是否显示</td>");
        strHtml.Append("<td class='table_title' nowrap>删除</td>");
        strHtml.Append("</tr>");
        System.Data.IDataReader dr = dbPage.ExecuteReader();
        int i = 0;
        while (dr.Read())
        {
            if (i == 0)
            {
                strHtml.Append("<tr class='table_td'>");
                i++;
            }
            else
            {
                strHtml.Append("<tr class='table_td2'>");
                i--;
            }
            strHtml.Append("<td>" + dr["LinkId"].ToString() + "</td>");
            strHtml.Append("<td><a target=_blank href='link_xq.aspx?LinkId=" + dr["LinkId"].ToString() + "'>" + dr["Title"].ToString() + "</a></td>");
           
            strHtml.Append("<td align='center'>" + dr["LinkWWW"].ToString() + "</td>");
            strHtml.Append("<td align='center'>" + dr["AddDate"].ToString() + "</td>");
            strHtml.Append("<td>" + dr["IsShow"].ToString() + "</td>");
            strHtml.Append("<td><input name='chk' type='checkbox' value='" + dr["LinkId"].ToString() + "'></td>");
            strHtml.Append("</tr>");
        }
        strHtml.Append("</table>");
        Label1.Text = strHtml.ToString();
        dr.Close();
        dr.Dispose();
        dbPage.Db.Close();
        db.Close();

        this.DbPageFlip1.DbPage = dbPage;//将数据传给控键

    }

    protected void Button5_Click(object sender, EventArgs e)//保存链接
    {
        HttpFileCollection files = HttpContext.Current.Request.Files;
        
        string LoadFileName = "";
        string fileName = "";
        string fileExtension = "";
        Boolean Useful = false;
        try
        {
            for (int iFile = 0; iFile < files.Count; iFile++)
            {
                ///'检查文件扩展名字

                HttpPostedFile postedFile = files[iFile];

                fileName = FileUpload1.FileName;
                LoadFileName = "";
                fileExtension = System.IO.Path.GetExtension(fileName);
                if (((fileExtension.ToUpper()) == ".JPG") || (fileExtension.ToUpper() == ".JPEG") || (fileExtension.ToUpper() == ".BMP") || (fileExtension.ToUpper() == ".GIF"))
                {
                    Useful = true;
                }
                else
                {
                    Useful = false;
                }
                if (fileName != "" && Useful)
                {
                    //LoadFileName = fileName;
                    LoadFileName = SysFun.GetIntDate() + "-" + SysFun.GetIntTime() + "-" + SysFun.GetRnd(10000, 99999);



                    ///注意：可能要修改你的文件夹的匿名写入权限。

                    postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath("../UploadFile/") + LoadFileName + fileExtension);


                }
            }
            if (Useful || fileName =="")
            {
                Db db = new Db();
                Grove.ORM.ObjectOperator oo = db.GetObjectOperator();
                HZ.MBSM.Model.Link Link = new HZ.MBSM.Model.Link();
                Link.Title = txtname.Text.ToString().Trim();
                Link.AccountId = 0;
                Link.AddDate = System.DateTime.Now;
                Link.DownLoad = LoadFileName + fileExtension;
                Link.LinkWWW = Textwww.Text.ToString().Trim();
                oo.Insert(Link);
                oo.Dispose();
                db.Close();
                
                
                strStatus.Text = "保存成功！";
             //   Response.Write("<script language=javascipt>location.href('LinkAdd.aspx');</scipt>");
                Response.Redirect("LinkAdd.aspx",true); 

            }
            else
            {
                strStatus.Text = "只能上传图片文件格式(.JPG　、.JPEG　、.BMP、.GIF)!"; 
            }
             
        }
        catch (System.Exception Ex)
        {
            strStatus.Text = Ex.Message;


        }
    }
    #region IPageDescribe 成员


    public string Describe
    {
        get { return ""; }
    }
    public string Name
    {
        get { return "添加友情链接"; }
    }
    public string ParentName
    {
        get { return "信息发布"; }
    }

    public System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> UseAccountType
    {
        get
        {
            System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();
            _AccountType.Add(HZ.MBSM.DAL.AccountType.enumAttorney);
            return _AccountType;
        }
    }

    #endregion
}
